#!/usr/bin/python
from config import *
import web
from lib import chkuser,loadHtmlFile,MC,chklogin
mc = MC(userDB)
class reg:
    def POST(self):
        chkres = self.chkuser()
        if(chkres):
            return chkres;
        i = web.input()
        if(i.user == ''):
            return self.GET();
        if(i.password != i.repassword):
            return "second enter password different to first";
        #user = i.user.encode('UTF-8');
        #passwd = i.password.encode('UTF-8');
        if(chkuser(user,mc) == 0):
            return "the user already exists"
        mc.set(user,passwd)
        html = loadHtmlFile('%sreg_res.html' % (htmlPath))
        return html
    def GET(self):
        chkres = self.chkuser()
        if(chkres):
            return chkres
        html = loadHtmlFile('%sreg.html' % (htmlPath))
        return html
    def chkuser(self):
        if(chklogin(web,mc)):
            return loadHtmlFile('%slogin_res.html'%(htmlPath)) 
        else:
            return None
